﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Oplaco.Web.DTO.IMPL
{
    /// <summary>
    /// Statistics collection class
    /// </summary>
    public class CachedStatisticsCollection : ICollection<CachedStatistics>
    {
        #region Members
        private bool isRO;
        private List<CachedStatistics> innerCollection;
        #endregion

        #region Public
        public void Add(CachedStatistics item)
        {
            if (!Contains(item))
            {
                innerCollection.Add(item);
            }
            else
            {
                Oplaco.Exceptions.AppService.ProcessAndNotifyDataAccessLayerException(new Exception("Item is already in the collection" + item.Id.ToString()));
            }
        }

        public void Clear()
        {
            innerCollection.Clear();
        }

        public bool Contains(CachedStatistics item)
        {
            if (innerCollection == null || item == null) return false;
            CachedStatistics found = (from c in innerCollection where c.Equals(item) select c).SingleOrDefault<CachedStatistics>();
            return (found == null) ? false : true;
        }

        public void CopyTo(CachedStatistics[] array, int arrayIndex)
        {
            innerCollection = (from item in array select item).ToList<CachedStatistics>();
        }

        public int Count
        {
            get
            {
                if (innerCollection == null) return -1;
                else return innerCollection.Count;
            }
        }

        public bool IsReadOnly
        {
            get { return isRO; }
        }

        public bool Remove(CachedStatistics item)
        {
            CachedStatistics found = (from c in innerCollection where c.Equals(item) select c).Single<CachedStatistics>();
            if (found == null) return false;
            innerCollection.Remove(found);
            return true;
        }

        public IEnumerator<CachedStatistics> GetEnumerator()
        {
            return new StatisticsCacheEnumerator(this);
        }
        #endregion

        #region Construct
        // Adds an index to the collection.
        public CachedStatistics this[int index]
        {
            get { return (CachedStatistics)innerCollection[index]; }
            set { innerCollection[index] = value; }
        }

        public CachedStatisticsCollection()
        {
            innerCollection = new List<CachedStatistics>();
        }


        #endregion

        IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new StatisticsCacheEnumerator(this);
        }

        #region Inner class
        /// <summary>
        /// Defines the enumerator for the CategoryCache collection.
        /// (Some prefer this class outside in the collection class.)
        /// </summary>
        public class StatisticsCacheEnumerator : IEnumerator<CachedStatistics>
        {
            private CachedStatisticsCollection _collection;
            private int curIndex;
            private CachedStatistics curItem;

            /// <summary>
            /// 
            /// </summary>
            /// <param name="collection"></param>
            public StatisticsCacheEnumerator(CachedStatisticsCollection collection)
            {
                _collection = collection;
                curIndex = -1;
                curItem = default(CachedStatistics);

            }

            public bool MoveNext()
            {
                //Avoids going beyond the end of the collection.
                if (++curIndex >= _collection.Count)
                {
                    return false;
                }
                else
                {
                    // Set current box to next item in collection.
                    curItem = _collection[curIndex];
                }
                return true;
            }

            public void Reset() { curIndex = -1; }

            void IDisposable.Dispose() { }

            public CachedStatistics Current
            {
                get { return curItem; }
            }


            object IEnumerator.Current
            {
                get { return Current; }
            }

        }
        #endregion
    }
}
